package com.ms.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author zdh
 */
public class DateUtil {

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static final int MILLIS_PER_SECOND = 1000;
    public static final int SECONDS_PER_MINUTE = 60;
    public static final int SECONDS_PER_HOUR = 3600;
    public static final int SECONDS_PER_DAY = 86400;
    public static final int MILLIS_PER_DAY = 86400000;
    public static final int MILLIS_PER_HOUR = 3600000;
    public static final int MILLIS_PER_MINUTE = 60000;
    public static final String YEAR_ONLY = "yyyy";
    public static final String YEAR_MONTH = "yyyy-MM";
    public static final String DATE_ONLY = "yyyy-MM-dd";
    public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_MILLS = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TIME_MILLS2 = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String DATE_SHORT_TIME = "yyyy-MM-dd HH:mm";
    public static final String TIME_ONLY = "HH:mm:ss";
    public static final String SHORT_TIME_ONLY = "HH:mm";
    public static final String DATEONLY = "yyyyMMdd";
    public static final String DATETIME = "yyyyMMddHHmmss";
    public static final String TIMEONLY = "HHmmss";
    public static final String DATEMILLS = "yyyyMMddHHmmssSSS";
    public static final int UNIT_SECOND = 1000;
    public static final int UNIT_MINUTE = 60000;
    public static final int UNIT_HOUR = 3600000;


    public static String format(String pattern, Date date) {
        if (date == null) {
            return "";
        } else {
            if (pattern==null || "".equals(pattern.trim())) {
                pattern = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = getDateFormat(pattern);
            return format(sdf, date);
        }
    }

    public static String formatDateTime(Date date) {
        return date == null ? "" : formatDateByPattern(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDateTimeMillis(Date date) {
        return date == null ? "" : formatDateByPattern(date, "yyyy-MM-dd HH:mm:ss.SSS");
    }

    public static String formatDateTimeMillis2(Date date) {
        return date == null ? "" : formatDateByPattern(date, "yyyy-MM-dd HH:mm:ss SSS");
    }

    public static String formatDate(Date date) {
        return formatDateByPattern(date, "yyyy-MM-dd");
    }


    public static String formatTime(Date date) {
        return formatDateByPattern(date, "HH:mm:ss");
    }
    public static String formatDateByPattern(Date date, String p) {
        return format(p, date);
    }


    public static SimpleDateFormat getDateFormat(String p) {
        if (p==null || "".equals(p.trim())) {
            p = "yyyy-MM-dd";
        }

        SimpleDateFormat sdf = createDateFormat(p);
        return sdf;
    }

    public static String format(SimpleDateFormat format, Date date) {
        Assert.notNull(format, "format can not be null");
        String rs = "";
        if (date == null) {
            return rs;
        } else {
            try {
                rs = format.format(date);
            } catch (Exception var4) {
                Exception e = var4;
                //logger.info("format date[{}] error: {}", date, e.getMessage());
                logger.info("format date:"+date+"error:"+e.getMessage());
            }

            return rs;
        }
    }

    public static SimpleDateFormat createDateFormat(String pattern) {
        Assert.hasText(pattern,"pattern must not be empty");
        //因为 SimpleDateFormat 非线程安全，需要每次创建实例
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        df.setTimeZone(TimeZone.getDefault());
        return df;
    }
}
